package com.john.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/cookie")
public class CookieController {

    @CrossOrigin
    @RequestMapping("/set")
    public String setCookie(String key, String val, HttpServletResponse resp, HttpServletRequest req) {
        Cookie cookie = new Cookie(key, val);
        cookie.setPath("/");
        resp.addCookie(cookie);

        System.out.println("添加Cookie: " + key+" ->" + val);
        return "index";
    }

    @RequestMapping("/del")
    public String delCookie(String key, HttpServletResponse resp, HttpServletRequest req) {

        Cookie cookie = new Cookie(key, null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        resp.addCookie(cookie);

        System.out.println("清除Cookie: " + key);
        return "index";
    }




}
